package com.reggie.service.impl;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.reggie.common.CustomException;
import com.reggie.dto.SetmealDto;
import com.reggie.entity.Dish;
import com.reggie.entity.Setmeal;
import com.reggie.entity.SetmealDish;
import com.reggie.mapper.SetmealMapper;
import com.reggie.service.DishService;
import com.reggie.service.SetmealDishService;
import com.reggie.service.SetmealService;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.function.Function;
import java.util.stream.Collectors;

@Service
@MapperScan("com.reggie.mapper")
public class SetmealServiceImpl extends ServiceImpl<SetmealMapper, Setmeal> implements SetmealService {
    @Autowired
    private SetmealDishService setmealDishService;
    @Autowired
    private SetmealMapper setmealMapper;
    @Autowired
    private DishService dishService;

    /*
     * //新增套餐，同时保存套餐和菜品的关联关系
     * */
    @Override
    @Transactional
    public void saveWithDish(SetmealDto setmealDto) {
        //保存套餐信息setmeal
        this.save(setmealDto);
        List<SetmealDish> setmealDishes = setmealDto.getSetmealDishes();
        setmealDishes =setmealDishes.stream().map(setmealDish -> {
            setmealDish.setSetmealId(setmealDto.getId());
            return setmealDish;
        }).collect(Collectors.toList());
        //保存套餐和菜品关联信息setmeal_dish
        setmealDishService.saveBatch(setmealDishes);
    }
    /*@Override
        public List<SetmealDto> page(int page, int pageSize) {
            List<SetmealDto> setmealDtos = setmealMapper.selectPage(page, pageSize);
            return setmealDtos;
        }*/
    /*
    * 删除套餐，同时删除套餐和菜品的关联；
    * */
    @Override
    @Transactional
    public void removeWithDish(List<Long> ids) {
        //查询套餐状态，确定是否可以删除
        LambdaQueryWrapper<Setmeal> queryWrapper=new LambdaQueryWrapper<>();
        queryWrapper.in(Setmeal::getId,ids).eq(Setmeal::getStatus,1);

        int count = this.count(queryWrapper);
        //不能删除，抛出一个业务异常
        if (count>0){
            throw new CustomException("套餐正在售卖中，不能删除");
        }
        //可以删除，先删除套餐表中数据
        this.removeByIds(ids);
        //删除关系表中的数据
        LambdaQueryWrapper<SetmealDish> lambdaQueryWrapper=new LambdaQueryWrapper<>();
        lambdaQueryWrapper.in(SetmealDish::getSetmealId,ids);
        setmealDishService.remove(lambdaQueryWrapper);
    }

    @Override
    public SetmealDto getByIdWithDish(Long id) {
        // 根据id查询setmeal表中的基本信息
        Setmeal setmeal = this.getById(id);
        SetmealDto setmealDto = new SetmealDto();
        // 对象拷贝。
        BeanUtils.copyProperties(setmeal, setmealDto);
        // 查询关联表setmeal_dish的菜品信息
        LambdaQueryWrapper<SetmealDish> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(SetmealDish::getSetmealId, id);
        List<SetmealDish> setmealDishList = setmealDishService.list(queryWrapper);
        //设置套餐菜品属性
        setmealDto.setSetmealDishes(setmealDishList);
        return setmealDto;
    }

    /*
    * 更新套餐，同时更新setmeal_dish
    * */
    @Override
    public void updateWithDish(SetmealDto setmealDto) {
        //更新setmeal信息
        LambdaUpdateWrapper<Setmeal> updateWrapper=new LambdaUpdateWrapper<>();
        updateWrapper.eq(Setmeal::getId,setmealDto.getId());
        this.update(setmealDto,updateWrapper);
        //删除setmeal_dish中的信息
        LambdaQueryWrapper<SetmealDish> queryWrapper=new LambdaQueryWrapper<>();
        queryWrapper.eq(SetmealDish::getSetmealId,setmealDto.getId());
        setmealDishService.remove(queryWrapper);
        //更新(重新保存)setmeal_dish中的信息
        List<SetmealDish> setmealDishes = setmealDto.getSetmealDishes();
        setmealDishes =setmealDishes.stream().map(setmealDish -> {
            setmealDish.setSetmealId(setmealDto.getId());
            return setmealDish;
        }).collect(Collectors.toList());
        setmealDishService.saveBatch(setmealDishes);
    }

    /*
    * 更新状态，需要查询订单里的setmeal_dish中dish_id对应的状态，只有为1才可以将自己的状态设置为1
    * */
    @Override
    public String updateStatus(Integer status, List<Long> ids) {
        if (status==1){
            //说明想要让套餐起售
            //根据id查询到对应的菜品的id
            for (Long id : ids) {
                LambdaQueryWrapper<SetmealDish> queryWrapper=new LambdaQueryWrapper<>();
                queryWrapper.eq(SetmealDish::getSetmealId,id);
                List<SetmealDish> setmealDishes = setmealDishService.list(queryWrapper);
                for (SetmealDish setmealDish : setmealDishes) {
                    Long dishId = setmealDish.getDishId();
                    Dish dish = dishService.getById(dishId);
                    if (dish.getStatus()==0){
                        return "修改失败,套餐中有菜品停售";
                    }
                }
                //走到这，说明对应菜品都在销售中，可以起售
            }
        }
        //如果是修改为0，直接修改
        LambdaUpdateWrapper<Setmeal> updateWrapper=new LambdaUpdateWrapper<>();
        updateWrapper.in(Setmeal::getId,ids).set(Setmeal::getStatus,status);
        this.update(updateWrapper);
        return "修改成功";
    }
}
